#include "MenuState.hpp"
#include "PlayState.hpp"

#include <CEGUI.h>
#include <RendererModules/Ogre/CEGUIOgreRenderer.h>
#include <OGRE\OgreLogManager.h>
#include <OGRE\OgreSceneManager.h>
#include <OGRE\OgreRoot.h>

void MenuState::Init()
{
	Ogre::LogManager::getSingletonPtr()->logMessage("Initiating MenuState...");

	Ogre::SceneManager* scene = Ogre::Root::getSingletonPtr()->createSceneManager(Ogre::ST_GENERIC, "MenuSceneManager");

	try
	{
		CEGUI::OgreRenderer* renderer = &CEGUI::OgreRenderer::bootstrapSystem();

		CEGUI::Logger::getSingleton().setLogFilename("CEGUI.log");
		CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton();

		CEGUI::SchemeManager::getSingleton().create("VanillaSkin.scheme");
		// set default mouse image
		CEGUI::System::getSingleton().setDefaultMouseCursor("Vanilla-Images", "MouseArrow");

		CEGUI::FontManager::getSingleton().create("GameDefault.font");
		CEGUI::FontManager::getSingleton().create("Cowboy-10.font");
		CEGUI::FontManager::getSingleton().create("Cowboy-5.font");
		CEGUI::ImagesetManager::getSingleton().createFromImageFile("BackgroundImage", "PlanetBackground.tga");

		m_pBackgroundStatic = winMgr.createWindow("Vanilla/StaticImage");
		m_pBackgroundStatic->setArea(CEGUI::URect(cegui_reldim(0),cegui_reldim(0),cegui_reldim(1),cegui_reldim(1)));
		m_pBackgroundStatic->setProperty("FrameEnabled", "false");
		m_pBackgroundStatic->setProperty("BackgroundEnabled", "false");
		m_pBackgroundStatic->setProperty("Image", "set:BackgroundImage image:full_image");
		CEGUI::System::getSingleton().setGUISheet(m_pBackgroundStatic);

		CEGUI::ImagesetManager::getSingleton().create("wsad.imageset");

		m_pBackgroundStatic->addChildWindow(winMgr.loadWindowLayout("MenuState.layout"));

		winMgr.getWindow("HelpWindow")->setVisible(false);
		//winMgr.getWindow("HelpWindow")->setProperty("FrameEnabled", "false");
		//winMgr.getWindow("HelpWindow")->setProperty("BackgroundEnabled", "false");

		winMgr.getWindow("NewGame")->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&MenuState::handleNewGame, this));
		winMgr.getWindow("Help")->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&MenuState::handleHelp, this));
		winMgr.getWindow("Exit")->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&MenuState::handleExit, this));

	} catch (const CEGUI::Exception& ex)
	{
		Ogre::LogManager::getSingleton().logMessage(ex.getMessage().c_str());
	}

	SetInitialized(true);
}

void MenuState::Run(double dt)
{
	CEGUI::System::getSingleton().injectTimePulse(dt/1000.f);
}

void MenuState::Pause()
{
	m_pBackgroundStatic->setVisible(false);
	CEGUI::MouseCursor::getSingleton().hide();
}

void MenuState::Resume()
{
	m_pBackgroundStatic->setVisible(true);
	CEGUI::MouseCursor::getSingleton().show();
}

void MenuState::Exit()
{

}

bool MenuState::keyPressed(const OIS::KeyEvent &keyEventRef)
{
	CEGUI::System::getSingleton().injectKeyDown(keyEventRef.key);
	CEGUI::System::getSingleton().injectChar(keyEventRef.text);
	return true;
}

bool MenuState::keyReleased(const OIS::KeyEvent &keyEventRef)
{
	CEGUI::System::getSingleton().injectKeyUp(keyEventRef.key);
	return true;
}

CEGUI::MouseButton convertButton(OIS::MouseButtonID buttonID)
{
    switch (buttonID)
    {
    case OIS::MB_Left:
        return CEGUI::LeftButton;
 
    case OIS::MB_Right:
        return CEGUI::RightButton;
 
    case OIS::MB_Middle:
        return CEGUI::MiddleButton;
 
    default:
        return CEGUI::LeftButton;
    }
}

bool MenuState::mouseMoved(const OIS::MouseEvent &evt)
{
	CEGUI::System &sys = CEGUI::System::getSingleton();
	sys.injectMouseMove(evt.state.X.rel, evt.state.Y.rel);
	// Scroll wheel.
	if (evt.state.Z.rel)
		sys.injectMouseWheelChange(evt.state.Z.rel / 120.0f);
	return true;
}

bool MenuState::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
	CEGUI::System::getSingleton().injectMouseButtonDown(convertButton(id));
	return true;
}

bool MenuState::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
	CEGUI::System::getSingleton().injectMouseButtonUp(convertButton(id));
	return true;
}

bool MenuState::handleNewGame(const CEGUI::EventArgs& args)
{
	ChangeState(1);
	m_pPlayState->reset();
	return true;
}

bool MenuState::handleHelp(const CEGUI::EventArgs& args)
{
	static bool flag = true;

	CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton();

	winMgr.getWindow("HelpWindow")->setVisible(flag);
	flag = !flag;
	return true;
}

bool MenuState::handleExit(const CEGUI::EventArgs& args)
{
	exit(0);
	return true;
}